<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;

/*网站前台头部脚部插入文件*/
class Script extends Base{

	/*列表*/
	public function index(Request $request){
		$this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $model=new \app\admin\model\Script();/*调用模型查询*/
        $model_data=$model->get_data();
        $data['data_list']=$model_data;
        $this->assign($data);
	    return $this->fetch();
	}

    /*新增 或者 修改*/
	public function change(Request $request){
		$this->check_session();
        $data=$request->post();
    	if(empty($data)){
          return $this->fetch();
        }else{
          if(empty($data['title'])){
            $result=['status'=>'0','msg'=>'请填写标题'];
            return json($result);
          }
          if(empty($data['content'])){
           	$result=['status'=>'0','msg'=>'请填写内容'];
           	return json($result);
          }
          if(empty($data['site'])){
           	$result=['status'=>'0','msg'=>'请选择位置'];
           	return json($result);
          }
          $model=new \app\admin\model\Script();/*调用模型查询*/
          $get=$request->get();
          if(!empty($get['id'])){
          	$model_data=$model->update_data($get['id'],$data['title'],$data['content'],$data['site']);
          }else{
          	$model_data=$model->insert_data($data['title'],$data['content'],$data['site']);
          }
          if($model_data==1){
	          	$result=['status'=>'1','msg'=>'操作成功'];
	           	return json($result);
           }else{
	           	$result=['status'=>'0','msg'=>'操作失败系统错误'];
	           	return json($result);
           }
        }
	}

    /*删除*/
	public function del(Request $request){
		$this->check_session();
		$data=$request->post();
		if(empty($data['id'])){
			$result=['status'=>'0','msg'=>'操作有误'];
	        return json($result);
		}
		$model=new \app\admin\model\Script();/*调用模型查询*/
        $model_data=$model->delete_data($data['id']);
		if($model_data!=1){
			$result=['status'=>'0','msg'=>'删除失败'];
	        return json($result);
		}else{
			$result=['status'=>'1','msg'=>'删除成功'];
	        return json($result);
		}
	}

}

